﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：DistributionRelationByCom.cs
    // 文件功能描述：根据配送企业查看需配送的医疗机构
    // 创建标识：吕海乐2009-04-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DistributionManage
{
    public partial class DistributionRelationByCom : BasePage
    {

        IBLL_Distribution id = Factory.BLL_Distribution();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        IBLL_FlowEngine ife = Factory.BLL_FlowEngine();

        DataTable _distibutionRelationTable;
        public DistributionRelationByCom()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"].ToString();
            ViewState["v_procureCatalogId"] = procureCatalogId;
            if (!IsPostBack)
            {
                Model.View.ProcureCatalog procureCatalog = ipb.GetProcureCatalogByID(procureCatalogId);
                lblCompany_SC.Text = procureCatalog.CompanyName_SC;
                lblCompany_ZB.Text = procureCatalog.CompanyName_TB;
                lblGoodsName.Text = procureCatalog.GoodsName;
                lblMaterial.Text = procureCatalog.Material;
                lblOutlookc.Text = procureCatalog.Outlookc;
                lblUnit.Text = procureCatalog.Unit;

                HZ.DrugTrade.Model.Query.DistributionRelation aDistributionRelation = new HZ.DrugTrade.Model.Query.DistributionRelation();
                //加入地区过滤条件，管理机构只能管理本地区下的采购单
                aDistributionRelation.AreaID = CurrentAccount.GetUser().Org.Area.AreaID;
                aDistributionRelation.ProcureCatalogID = procureCatalogId;
                aDistributionRelation.SortFields = "CompanyName_PS asc";

                int totalCount = 0;
                IList<HZ.DrugTrade.Model.View.DistributionRelation> distibutionRelationList = id.GetDistributionByWhere(aDistributionRelation, 1, 0, out totalCount);
                _distibutionRelationTable = HZ.Data.ModelConvert.GetDataTableFromModel(distibutionRelationList);

                BindCompany();
            }
        }

        /// <summary>
        /// 绑定该产品的所有配送企业
        /// </summary>
        private void BindCompany()
        {
            int totalCount = 0;
            IList<HZ.DrugTrade.Model.View.ProcureCatalog> procureCatalogList = ipb.GetProcureCatalogByWhere(new HZ.DrugTrade.Model.Query.ProcureCatalog() { ProcureCatalogID = ViewState["v_procureCatalogId"].ToString(), SortFields = "ProcureCatalogID asc" }, 1, 0, out totalCount);
            if (procureCatalogList.Count > 0)
            {

                string companyNames = procureCatalogList[0].CompanyNames_PS;
                string companyIds = procureCatalogList[0].CompanyIDs_PS;
                string[] arrayNames = companyNames.Split(',');
                string[] arrayIds = companyIds.Split(',');

                IList<Company> myList = new List<Company>();
                for (int i = 0; i < arrayNames.Length; i++)
                {
                    Company myCompany = new Company();
                    myCompany.CompanyName = arrayNames[i];
                    myCompany.CompanyID = arrayIds[i];
                    myCompany.CompanyType = HZ.DrugTrade.Model.Enum.EnumCompanyType.配送企业;
                    myList.Add(myCompany);
                }

                rptCompany.DataSource = myList;
                rptCompany.DataBind();
            }
            else
            {
                PageError("没有配送企业",this.ReturnUrl);
            }
        }

        /// <summary>
        /// 绑定某配送企业的所有医疗机构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptCompany_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataTable table;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this._distibutionRelationTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    //dv.RowFilter = "CompanyName_PS='" + DataBinder.Eval(e.Item.DataItem, "CompanyName").ToString() + "'";
                    dv.RowFilter = "CompanyID_PS='" + DataBinder.Eval(e.Item.DataItem, "CompanyID") + "'";
                    Repeater rptHospital = (Repeater)e.Item.FindControl("rptHospital");
                    rptHospital.DataSource = dv;
                    rptHospital.DataBind();
                }
            }
        }

        /// <summary>
        /// 绑定配送的关系的流程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptHospital_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal literal = e.Item.FindControl("ltlFlow") as Literal;
                string flowID = literal.Text;
                Flow flow = ife.GetFlows(flowID);
                literal.Text = flow.FlowName;
            }
        }

        protected void rptHospital_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string str = e.CommandArgument.ToString();
                string[] array = str.Split(',');
                string isUsing = array[3];
                if (isUsing == false.ToString())
                {
                    DistributionRelation distributionRelation = new DistributionRelation();
                    distributionRelation.ProcureCatalogID = array[0];
                    distributionRelation.HospitalID = array[1];
                    distributionRelation.CompanyID_PS = array[2];
                    if (id.DelDistribution(distributionRelation)==1)
                    {
                        PageInfo("删除成功！",Request.RawUrl);
                        BindCompany();
                    }
                }
                else
                {
                    PageError("此医疗机构正在使用中，无法删除！",Request.RawUrl);
                }
            }
        }
    }
}
